import { createApp } from 'vue'
// 导入字体图标
import './assets/fonts/iconfont.css'
// 导入全局样式表
import './assets/css/global.css'
import App from './App.vue'
import router from './router'
import store from './store'

import axios from "axios";
// 统一导入el-icon图标
import * as ElIconModules from '@element-plus/icons'

let app=createApp(App)
app.config.globalProperties.axios=axios

// 统一注册el-icon图标
for(let iconName in ElIconModules){
    app.component(iconName,ElIconModules[iconName])
}

router.beforeEach((to, from, next) => {
    if (to.path === '/' || to.path === '/register'|| to.path === '/admin') {
        next();
    } else {
        const token = window.sessionStorage.getItem('token')
        if (!token) {
            next('/');
        } else {
            if(to.matched.length===0){
                next('/404')
            }else{
                next();
            }
        }
    }
});
// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
        //此处读取上文存储的token并设置请求头
        config.headers.Authorization = window.sessionStorage.getItem('token')
        // 这里需要注意，必须return config才能生效
        return config
    },
    error => {
        return Promise.reject(error);
    }
);

app.use(store).use(router)
app.mount('#app')
